<?php

class dbaOrder{

  function dbaOrder (& $dba)
  {
    $this->dba = &$dba;
  }
  
  function findPasswordByEmail ($email)
  {
    $sth = $this->dba->query("select pwd from ".$this->dba->tableforwarder."_shopCustomer" .
            " where email=".$this->dba->quote($email).";");
    if (is_object($sth))
    {
        $row = $sth->fetch_row();
        return $row[0];
    }
  }

  function getDelivererByOrder ($o_id)
  {
     $sth = $this->dba->query("select S.name from ".$this->dba->tableforwarder
        ."_shopOrder O, ".$this->dba->tableforwarder
        ."_shopSupplier S where O.supplier_id=S.id and O.id=$o_id");
     if (is_object($sth))
     {
        $row = $sth->fetch_row();
        $sth->free();
        return $row[0];
     } 
  }

  function getPaymentModeByOrder ($o_id)
  {
     $sth = $this->dba->query("select P.descr from ".$this->dba->tableforwarder
        ."_shopOrder O, ".$this->dba->tableforwarder
        ."_shopPaymentMode P where O.pm_id=P.id and O.id=$o_id");
     if (is_object($sth))
     {
        $row = $sth->fetch_row();
        $sth->free();
        return $row[0];
     } 
  }

  // Sektion Order :
  // Methoden zur Bestellungsverwaltung

  function addOrderCMS(&$customer,&$s_cart){
     $timestamp = time();
     $check = array('firma', 'name', 'vorname', 'strasse', 'plz', 'ort', 'land', 'bank', 'konto', 'blz');
     $cust = array();
     foreach ($check as $c)
     {
        $cust[$c] = (!empty($customer['CUST_OR'.strtoupper($c)]))?
            $customer['CUST_OR'.strtoupper($c)]:
            $customer['CUST_'.strtoupper($c)];
     }
     $cust['id']     = $customer['CUST_ID'];
     $cust["paycost_amount"] = ($customer['premium'])?$customer['premium']:$customer['discount'];
     $this->dba->query("insert into ".$this->dba->tableforwarder."_shopOrder " .
            "(firma,name,vorname,strasse,plz,ort,land,customer_id,bank_nr,bank_blz," .
            "bank_institut,tax_amount, total_amount, postage, paycost_amount , " .
            "currency, supplier_id, pm_id, timestamp,status) values(".$this->dba->quote($cust["firma"]).",".
            $this->dba->quote($cust["name"]).",".$this->dba->quote($cust["vorname"]).",".
            $this->dba->quote($cust["strasse"]).",".$this->dba->quote($cust["plz"]).",".
            $this->dba->quote($cust["ort"]).",".$this->dba->quote($cust["land"]).",".$cust['id'].",".
            $this->dba->quote($cust["konto"]).",".$this->dba->quote($cust["blz"]).",".
            $this->dba->quote($cust["bank"]).",".$this->dba->quote($customer["totalTax"]).",".
            $this->dba->quote($customer["totalPrice"]).",".$this->dba->quote($customer["postage"]).",".
            $this->dba->quote($cust["paycost_amount"]).",".$this->dba->quote($customer["stdCurrency"]).
            ",".$this->dba->quote($customer["CUST_DELIVERER"]).",".
            $this->dba->quote($customer["CUST_PAYMENTMODE"]) . "," . $timestamp .",'ordered');");
     print $this->dba->getError();
     $oid       = $this->dba->insert_id();
     while (list( , $value) = each($s_cart)){
       $this->dba->query("insert into ".$this->dba->tableforwarder."_shopOrderArt " .
            "(order_id,parent_id,article_id, name, orderno,quantity,amount,kg,packet,tax) values (".
            $oid.",NULL,".$value['id'].",".$this->dba->quote($value['name']).",".$this->dba->quote($value['orderno']).
            ",".$this->dba->quote($value['quantity']).",".$this->dba->quote($value['single_price']).",".
            $this->dba->quote($value['kg']).",".$this->dba->quote($value['packet']).
            ",".$this->dba->quote($value['tax']).");");
     print $this->dba->getError();
       if ($value['cnfart']){
         $parent_id = $this->dba->insert_id();
         foreach ($value['cnfart'] As $cnfart){
           $this->dba->query("insert into ".$this->dba->tableforwarder."_shopOrderArt (order_id,parent_id,article_id, name, orderno,quantity,amount,kg,packet,tax) values (".$oid.",".$parent_id.",".$cnfart['id'].",'".$cnfart['name']."','".$cnfart['orderno']."','".$value['quantity']."','".$cnfart['price']."','".$cnfart['kg']."','N','".$cnfart['tax']."');");
         }
       }
     }
  }

  function addOrder(&$s_customer,&$s_cart){
     $timestamp = time();
     $this->dba->query("insert into ".$this->dba->tableforwarder."_shopOrder (firma,name,vorname,strasse,plz,ort,land,customer_id,bank_nr,bank_blz,bank_institut,tax_amount, total_amount, postage, paycost_amount , currency,timestamp,status) values('".$s_customer["orfirma"]."','".$s_customer["orname"]."','".$s_customer["orvorname"]."','".$s_customer["orstrasse"]."','".$s_customer["orplz"]."','".$s_customer["orort"]."','".$s_customer["orland"]."',".$s_customer['id'].",'".$s_customer["banknr"]."','".$s_customer["bankzahl"]."','".$s_customer["bankinst"]."','".$s_customer["tax_amount"]."','".$s_customer["total_amount"]."','".$s_customer["postage"]."','".$s_customer["paycost_amount"]."','".$s_customer["currency"]."',".$timestamp.",'ordered');");
     print $this->dba->getError();
     $oid       = $this->dba->insert_id();
     while (list( , $value) = each($s_cart)){
       $this->dba->query("insert into ".$this->dba->tableforwarder."_shopOrderArt (order_id,parent_id,article_id, name, orderno,quantity,amount,kg,packet,tax) values (".$oid.",NULL,".$value['id'].",'".$value['name']."','".$value['orderno']."','".$value['quantity']."','".$value['single_price']."','".$value['kg']."','".$value['packet']."','".$value['tax']."');");
     print $this->dba->getError();
       if ($value['cnfart']){
         $parent_id = $this->dba->insert_id();
         foreach ($value['cnfart'] As $cnfart){
           $this->dba->query("insert into ".$this->dba->tableforwarder."_shopOrderArt (order_id,parent_id,article_id, name, orderno,quantity,amount,kg,packet,tax) values (".$oid.",".$parent_id.",".$cnfart['id'].",'".$cnfart['name']."','".$cnfart['orderno']."','".$value['quantity']."','".$cnfart['price']."','".$cnfart['kg']."','N','".$cnfart['tax']."');");
         }
       }
     }
  }

  function getOrder($id){
    $fieldStr = $this->dba->tableforwarder."_shopOrder.timestamp,".$this->dba->tableforwarder."_shopCustomer.firma,".$this->dba->tableforwarder."_shopCustomer.name,".$this->dba->tableforwarder."_shopCustomer.vorname,".$this->dba->tableforwarder."_shopCustomer.strasse,".$this->dba->tableforwarder."_shopCustomer.plz,".$this->dba->tableforwarder."_shopCustomer.ort,".$this->dba->tableforwarder."_shopCustomer.email,".$this->dba->tableforwarder."_shopCustomer.tel,".$this->dba->tableforwarder."_shopCustomer.fax,".$this->dba->tableforwarder."_shopCustomer.land,".$this->dba->tableforwarder."_shopOrder.firma As orfirma,".$this->dba->tableforwarder."_shopOrder.name As orname,".$this->dba->tableforwarder."_shopOrder.vorname As orvorname,".$this->dba->tableforwarder."_shopOrder.strasse As orstrasse,".$this->dba->tableforwarder."_shopOrder.plz As orplz,".$this->dba->tableforwarder."_shopOrder.ort As orort,".$this->dba->tableforwarder."_shopOrder.land As orland,".$this->dba->tableforwarder."_shopOrder.bank_nr,".$this->dba->tableforwarder."_shopOrder.bank_institut,".$this->dba->tableforwarder."_shopOrder.bank_blz, tax_amount , total_amount , postage , paycost_amount , currency";
    $request = "select $fieldStr from ".$this->dba->tableforwarder."_shopOrder,".$this->dba->tableforwarder."_shopCustomer where ".$this->dba->tableforwarder."_shopOrder.id = $id AND ".$this->dba->tableforwarder."_shopCustomer.id = ".$this->dba->tableforwarder."_shopOrder.customer_id;";
    $check = $this->dba->query($request);
    if ($check){
       $orderdata["main"]  = $this->dba->fetch_row_array();
       $request = "select * from ".$this->dba->tableforwarder."_shopOrderArt where order_id = $id;";
       $check2 = $this->dba->query($request);
       if ($check2){
        $orderdata["items"] = $this->dba->fetch_rows_Array();
        return $orderdata;
       } else {
          echo "Fehler bei _shopOrderArt<br>\n";
       }
    } else {
      echo "Fehler bei _shopOrder\n";
    }
  }

  function getNumOrderArt($order_id){
    $this->dba->query("select quantity from ".$this->dba->tableforwarder."_shopOrderArt where order_id = $order_id;");
    $data  = $this->dba->fetch_rows_array();
    for ($i=0;$i<count($data);$i++){
      $dcount += $data[$i]['quantity'];
    }
    $num[] = $this->dba->num_rows();
    $num[] = $dcount;
    return $num;
  }
  
  function getOrderList(&$result,$status){
    $check  = $this->dba->query("select * from ".$this->dba->tableforwarder."_shopOrder where status = '$status' order by timestamp;");
    if ($check){
     $result = $this->dba->fetch_rows_array();
    }
    return $check;
  }
  
  function getOrderListByCustomer(&$result,$customer_id,$status = ''){
    if ($status){
      $a .= " AND status='$status'";
    }
    $request = "select * from ".$this->dba->tableforwarder."_shopOrder where customer_id = $customer_id $a;";
    $check   = $this->dba->query($request);
    $result  = $this->dba->fetch_rows_array();
    return $check;    
  }
  
  function delOrder($order_id){
    $check = $this->dba->query("delete from ".$this->dba->tableforwarder."_shopOrder where id=$order_id;");
    if ($check){
      return $this->dba->query("delete from ".$this->dba->tableforwarder."_shopOrderArt where order_id=$order_id;");
    }
  }
  
  function registerOrder($id){
    return $this->dba->query("update ".$this->dba->tableforwarder."_shopOrder set status = 'registered' where id=$id;");
  }

  function finishOrder($id){
    return $this->dba->query("update ".$this->dba->tableforwarder."_shopOrder set status = 'finished', bank_nr = '', bank_blz = '', bank_institut = '' where id=$id;");
  }
  
  // Sektion Customer :
  // Methoden zur Kundenverwaltung
    
  function getCustomer($id,$fields = ''){
    if ($fields){
      $fieldStr = $this->dba->genFieldStr($fields);
    } else {
      $fieldStr = '*';
    } 
    $check = $this->dba->query("select $fieldStr from ".$this->dba->tableforwarder."_shopCustomer where id=$id;");
    if ($check){
      return $this->dba->fetch_row_array();      
    }
  }

  function getCustomerAuth($email,$pwd){
    $check = $this->dba->query("select * from ".$this->dba->tableforwarder."_shopCustomer where email='$email' AND pwd='$pwd';");
    if ($check){
      if ($this->dba->num_rows() > 0){
        return $this->dba->fetch_row_array();
      }
    }
  }
  
  function getCustomersFields(&$result,$fields){
    $check  = $this->dba->query("select ".$this->dba->genFieldStr($fields)." from ".$this->dba->tableforwarder."_shopCustomer;");
    $result = $this->dba->fetch_rows_array();
    return $check;
  }
  
  // suchen in Customer : $result->liefert ergebnisse, $fields: zu leifernde Felder, $search: ass array mit suchfeldern, $bool und/oder
  function searchCustomersFields(&$result,$fields,$search,$bool){
    reset($search);
    while(list($name,$value) = each($search)){
      $searchsql .= "$name like '%$value%' $bool";
    }
    $searchsql = preg_replace('/ '.$bool.'$/',"",$searchsql);
    $this->dba->query("select ".$this->dba->genFieldStr($fields)." from ".$this->dba->tableforwarder."_shopCustomer where $searchsql;");
  }
  
  function delCustomer($id){
    return $this->dba->query("delete from ".$this->dba->tableforwarder."_shopCustomer where id=$id;");
  }

  function addCustomer($c){
    $check = $this->dba->query("insert into ".$this->dba->tableforwarder."_shopCustomer (email,pwd,firma,name,vorname,strasse,plz,ort,land,tel,fax,orfirma,orname,orvorname,orstrasse,orplz,orort,orland) values ('".$c['email']."','".$c['pwd']."','".$c['firma']."','".$c['name']."','".$c['vorname']."','".$c['strasse']."','".$c['plz']."','".$c['ort']."','".$c['land']."','".$c['tel']."','".$c['fax']."','".$c['orfirma']."','".$c['orname']."','".$c['orvorname']."','".$c['orstrasse']."','".$c['orplz']."','".$c['orort']."','".$c['orland']."');");
    if ($check) return $this->dba->insert_id();
  }

  /**
   * @return array worked?, error/insert_id
   */
  function addCustomerCMS($c){
    $check = $this->dba->query("insert into ".$this->dba->tableforwarder."_shopCustomer" .
            " (email,pwd,firma,name,vorname,strasse,plz,ort,land,tel,fax,orfirma," .
            "orname,orvorname,orstrasse,orplz,orort,orland) values ('".$c['CUST_EMAIL'].
            "','".$c['CUST_PWD']."','".$c['CUST_FIRMA']."','".$c['CUST_NAME']."','".
            $c['CUST_VORNAME']."','".$c['CUST_STRASSE']."','".$c['CUST_PLZ']."','".
            $c['CUST_ORT']."','".$c['CUST_LAND']."','".$c['CUST_TEL']."','".
            $c['CUST_FAX']."','".$c['CUST_ORFIRMA']."','".$c['CUST_ORNAME']."','".
            $c['CUST_ORVORNAME']."','".$c['CUST_ORSTRASSE']."','".$c['CUST_ORPLZ']."','".
            $c['CUST_ORORT']."','".$c['CUST_ORLAND']."');");
    if ($errorno = $this->dba->getErrorNo()) 
    {
        return array(false, $errorno);
    }
    if ($check) return array(true, $this->dba->insert_id());
  }

  function updateCustomerCMS($c){
    $sth = $this->dba->query("update ".$this->dba->tableforwarder."_shopCustomer" .
            " set email='".$c['CUST_EMAIL']."',firma='".$c['CUST_FIRMA'].
            "',orfirma='".$c['CUST_ORFIRMA']."',name='".$c['CUST_NAME'].
            "',vorname='".$c['CUST_VORNAME']."',strasse='".$c['CUST_STRASSE'].
            "',plz='".$c['CUST_PLZ']."',ort='".$c['CUST_ORT']."',land='".$c['CUST_LAND'].
            "',tel='".$c['CUST_TEL']."',fax='".$c['CUST_FAX']."',orname='".$c['CUST_ORNAME'].
            "',orvorname='".$c['CUST_ORVORNAME']."',orstrasse='".$c['CUST_ORSTRASSE'].
            "',orplz='".$c['CUST_ORPLZ']."',orort='".$c['CUST_ORORT'].
            "',orland='".$c['CUST_ORLAND']."' where id = ".$c['CUST_ID'].";");
    return $this->dba->getErrorNo();
  }

  function updateCustomer($c){
    return $this->dba->query("update ".$this->dba->tableforwarder."_shopCustomer set email='".$c['email']."',pwd='".$c['pwd']."',firma='".$c['firma']."',orfirma='".$c['orfirma']."',name='".$c['name']."',vorname='".$c['vorname']."',strasse='".$c['strasse']."',plz='".$c['plz']."',ort='".$c['ort']."',land='".$c['land']."',tel='".$c['tel']."',fax='".$c['fax']."',orname='".$c['orname']."',orvorname='".$c['orvorname']."',orstrasse='".$c['orstrasse']."',orplz='".$c['orplz']."',orort='".$c['orort']."',orland='".$c['orland']."' where id = ".$c['id'].";");
  }


}

?>